package huaweiod.first;

import java.util.Arrays;
import java.util.List;
import java.util.Scanner;
import java.util.stream.Collectors;

/**
 * 华为机试真题 Java 实现【优选核酸检测点】【2022.11 Q4 新题】
 */
public class A_2023_32_PreferenceNucleicAcid {

    // 核酸点结构
    public static class Point {
        int id;
        //距离
        int dis;
        //排队人数
        int que;
        //总花费金钱
        int pay;
        //总花费时间
        int time;
    }

    // 人数增加情况
    public static final int[] add8_10 = {3, 480, 600};
    public static final int[] add12_14 = {10, 720, 840};

    public static void main(String[] args) {
        // 处理输入
        Scanner in = new Scanner(System.in);
        // 先将时间转换为分钟数
        int start = in.nextInt() * 60 + in.nextInt();
        int end = in.nextInt() * 60 + in.nextInt();
        int n = in.nextInt();

        // 构造核酸点
        Point[] points = new Point[n];
        for (int i = 0; i < points.length; i++) {
            Point point = new Point();
            point.id = in.nextInt();
            point.dis = in.nextInt();
            point.que = in.nextInt();
            point.pay = point.dis * 10;
            // 计算总耗时
            point.time = cal_time(start, point);
            points[i] = point;
        }

        List<Point> res = Arrays.stream(points).filter(p -> start + p.time <= end).sorted((a, b) -> {
            if (a.pay != b.pay) {
                return a.pay - b.pay;
            } else {
                if (a.time != b.time) {
                    return a.time - b.time;
                } else {
                    return a.id - b.id;
                }
            }
        }).collect(Collectors.toList());

        System.out.println(res.size());
        for (Point p : res) {
            System.out.print(p.id);
            System.out.print(" ");
            System.out.print(p.time);
            System.out.print(" ");
            System.out.println(p.pay);
        }
    }

    public static int cal_time(int start, Point point) {
        int total = 0;
        point.que = Math.max(0, point.que - point.dis * 10);
        total += point.dis * 10;
        // 到达时间
        int on = start + point.dis * 10;
        if(on <= add8_10[1]) {
            return add8_10[1] - start;
        } else if(on <= add8_10[2]) {
            point.que += (on - add8_10[1]) * (add8_10[0] - 1);
            return total + point.que;
        } else if(on <= add12_14[1]) {
            return total + point.que;
        } else if(on <= add12_14[2]) {
            point.que += (on - add12_14[1]) * (add12_14[0] - 1);
            return total + point.que;
        } else {
            return total + point.que;
        }
    }
}
